package com.example.ykbboom.universitytransportation.View;

import android.content.Context;
import android.app.AlertDialog;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ykbboom.universitytransportation.POJO.Distance;
import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.LogUtil;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ykbboom on 2017/3/11 20:46
 * email：ykbpro@whut.edu.cn
 */

public class DistanceDialog extends AlertDialog {
    private static final String TAG = DistanceDialog.class.getSimpleName();

    private Context context;
    private android.app.AlertDialog alertDialog;
    private List<Distance>distanceList;
    private String[]datas;
    private PickerScrollView pickerScrollView;
    private TextView titleView;
    private LinearLayout buttonLayout;
    private PickerScrollView.onSelectListener pickerListener;
    /**
     * 初始化数据
     */
    private void initData() {
        distanceList = new ArrayList<Distance>();
        datas = new String[] { "2000", "3000", "1000", "800", "500", "5000" };
        for (int i = 0; i < datas.length; i++) {
            distanceList.add(new Distance(datas[i]));
        }
        // 设置数据，默认选择第一条
        pickerScrollView.setData(distanceList);
        pickerScrollView.setSelected(0);
    }
    public DistanceDialog(Context context){
        super(context);
        // TODO Auto-generated constructor stub
        this.context=context;
        alertDialog=new android.app.AlertDialog.Builder(context).create();
        alertDialog.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.dialog_distance);
        titleView = (TextView) window.findViewById(R.id.dialog_distance_title);
        buttonLayout=(LinearLayout)window.findViewById(R.id.dialog_distance_btn_ll);
        pickerScrollView = (PickerScrollView) window.findViewById(R.id.picker_scroll_view);
        // 滚动选择器选中事件
        pickerListener = new PickerScrollView.onSelectListener() {
            @Override
            public void onSelect(Distance distance) {
                LogUtil.i(TAG, distance.getDistance());
                SharedPrefUtil.saveTempCheckDistance(Integer.parseInt(distance.getDistance()));
            }
        };
        pickerScrollView.setOnSelectListener(pickerListener);
        initData();
    }
    public void setTitle(int resId)
    {
        titleView.setText(resId);
    }
    public void setTitle(String title) {
        titleView.setText(title);
    }
    /**
     * 设置按钮
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text,final View.OnClickListener listener)
    {
        Button button=new Button(context);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
//        button.setBackgroundResource(R.drawable.alertdialog_button);
        button.setText(text);
        button.setTextColor(Color.WHITE);
        button.setTextSize(20);
        button.setOnClickListener(listener);
        buttonLayout.addView(button);
    } 	/**
     * 设置按钮
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text,final View.OnClickListener listener)
    {
        Button button=new Button(context);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
//        button.setBackgroundResource(R.drawable.alertdialog_button);
        button.setText(text);
        button.setTextColor(Color.WHITE);
        button.setTextSize(20);
        button.setOnClickListener(listener);
        if(buttonLayout.getChildCount()>0)
        {
            params.setMargins(20, 0, 0, 0);
            button.setLayoutParams(params);
            buttonLayout.addView(button, 1);
        }else{
            button.setLayoutParams(params);
            buttonLayout.addView(button);
        } 	}
    /**
     * 关闭对话框
     */
    public void dismiss() {
        alertDialog.dismiss();
    }
}
